package com.itheima.controller;

import com.itheima.aspect.Log;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/students")
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result getAllStudent(StudentQueryParam sqp){
        PageBean pageBean =  studentService.getAllStudent(sqp);
        return Result.success(pageBean);
    }

    @PostMapping
    @Log
    public Result addStudent(@RequestBody Student student){
        studentService.addStudent(student);
        return Result.success();
    }


    /**
     *根据id回显学生信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getStudent(@PathVariable Integer id){
        Student student =  studentService.getStudentById(id);
        return Result.success(student);
    }

    /**
     * 修改学生信息
     * @param student
     * @return
     */
    @PutMapping
    @Log
    public Result modifyStudent(@RequestBody Student student){
        studentService.modifyStudent(student);
        return Result.success();
    }

    /**
     * 根据id批量删除学生
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    @Log
    public Result deleteStudentsByIds(@PathVariable List<Integer> ids){
        studentService.deleteStudents(ids);
        return Result.success();
    }

    /**
     * 违纪处理
     * @param id
     * @param score
     * @return
     */
    @PutMapping("/violation/{id}/{score}")
    @Log
    public Result violationExe(@PathVariable Integer id ,@PathVariable Integer score){
        studentService.violationExe(id,score);
        return Result.success();
    }

}
